<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!defaultAddress" class="none">您需要先添加收货地址才可提交订单。</div>
      <template v-else>
      <ul>
        <li><span>收<i/>货<i/>人：</span>{{defaultAddress.receiver}}</li>
        <li><span>联系方式：</span>{{defaultAddress.contact}}</li>
        <li><span>收货地址：</span>{{defaultAddress.fullLocation.replace(/ /g,'')+defaultAddress.address}}</li>
      </ul>
      <a @click="openAddressEdit(defaultAddress)" href="javascript:;">修改地址</a>
      </template>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="dialogVisible=true">切换地址</XtxButton>
      <XtxButton @click="openAddressEdit()" class="btn">添加地址</XtxButton>
    </div>
  </div>
  <!-- 对话框组件 -->
  <XtxDialog title="切换收货地址" v-model="dialogVisible">
     <div class="text item" v-for="item in list" :key="item.id" :class="{ active: item.id === activeId }" @click="changeAddress(item.id)"  >
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
        </ul>
      </div>
     <template #footer>
        <XtxButton @click="dialogVisible=false" type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton @click="confirmAddress()" type="primary">确认</XtxButton>
      </template>
  </XtxDialog>
  <!-- 添加地址 -->
   <AddressEdit ref="addressEdit" @on-success="getAddress"></AddressEdit>
</template>
<script>
import { computed, inject, ref, watch } from 'vue'
import AddressEdit from './address-edit.vue'
export default {
  components: { AddressEdit },
  name: 'CheckoutAddress',
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    // 对话框显示隐藏
    const dialogVisible = ref(false)
    // 添加收货地址组件
    const addressEdit = ref(false)
    const data = inject('data')
    // 添加收货地址和修改收货地址
    // 调用修改地址组件内部的open事件, 传入需要修改的地址对象
    const openAddressEdit = (address) => {
      addressEdit.value.open(address)
    }
    // 新的收货地址
    const newAddress = ref(null)
    // 显示到页面中的收货地址
    const defaultAddress = computed(() => {
      // 用计算属性, 动态获取地址列表中默认地址
      // isDefault 表示是否为默认 0为默认
      // 如果没有添加或修改新的收货地址
      if (!newAddress.value) {
        // 选择默认地址
        return props.list.find(item => item.isDefault === 0)
      } else {
        // 有新地址就显示新地址
        return newAddress.value
      }
    })
    // 获取用户添加或修改的地址
    const getAddress = (formData) => {
      if (formData.receiver) {
        newAddress.value = { ...formData }
      }
    }
    // 实现默认高亮效果
    const activeId = ref(defaultAddress.value.id)
    const changeAddress = (theId) => {
      console.log(theId)
      activeId.value = theId
    }
    // 确认收货地址
    const confirmAddress = () => {
      newAddress.value = props.list.find(item => item.id === activeId.value)
      dialogVisible.value = false
    }
    // id 直接从页面显示的收货地址中获取，变化的时候也会自动监听重新获取
    watch(defaultAddress, ({ id }) => {
      // 收集提交订单接口所需的收货地址 id
      data.addressId = id
    }, { immediate: true })
    return { dialogVisible, addressEdit, openAddressEdit, defaultAddress, getAddress, changeAddress, activeId, confirmAddress }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>
